package com.woniu.bh.beautysalon.web.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.bh.beautysalon.model.dto.BeautysalonMoneyDto;
import com.woniu.bh.beautysalon.model.dto.DailyBudgetDto;
import com.woniu.bh.beautysalon.model.dto.QuarterIncomeDto;
import com.woniu.bh.beautysalon.model.params.BeautysalonPlatformflowsParam;
import com.woniu.bh.beautysalon.web.common.DateUtils;
import com.woniu.bh.beautysalon.web.dao.OrdersMapper;
import com.woniu.bh.beautysalon.web.dao.PlatformFlowsMapper;
import com.woniu.bh.beautysalon.web.service.IPlatformFlowsService;
import com.woniu.bh.common.core.domain.backstage.PlatformFlows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author daniel
 * @since 2020-10-16
 */
@Service
public class PlatformFlowsServiceImpl extends ServiceImpl<PlatformFlowsMapper, PlatformFlows> implements IPlatformFlowsService {

    @Autowired
    private PlatformFlowsMapper platformFlowsMapper;

    /**
     * @description: 日常流水
     * @param: beautysalonId
     * @return: com.woniu.bh.beautysalon.model.dto.DailyBudgetDto
     */
    @Override
    public DailyBudgetDto dailyBudget(Integer beautysalonId) {
        //获取今日起始时间
        String curDateTime = DateUtils.getCurDateTime();
        System.out.println(curDateTime);
        Date beforeDate = DateUtils.getDate(curDateTime);
        Date afterDate = DateUtils.getDate(curDateTime);
        Calendar instance = Calendar.getInstance();
        //设置一天开始时间
        instance.setTime(beforeDate);
        instance.set(Calendar.HOUR_OF_DAY, 0);
        instance.set(Calendar.MINUTE, 0);
        instance.set(Calendar.SECOND, 1);
        beforeDate = instance.getTime();
        //设置一天结束时间
        instance.setTime(afterDate);
        instance.set(Calendar.HOUR_OF_DAY, 23);
        instance.set(Calendar.MINUTE, 59);
        instance.set(Calendar.SECOND, 59);
        afterDate = instance.getTime();

        QueryWrapper<PlatformFlows> platformFlowsQueryWrapper = new QueryWrapper<>();
        platformFlowsQueryWrapper.eq("platform_flows_beautysalon_id", beautysalonId);
        List<PlatformFlows> platformFlows = platformFlowsMapper.selectList(platformFlowsQueryWrapper);
        double totalMoney = 0;
        for (PlatformFlows p : platformFlows) {
            Date date = new Date(p.getPlatformFlowsTime());
            String timeStr = DateUtils.dateToDateString(date);
            Date formDate = DateUtils.getDate(timeStr);
            if (beforeDate.before(formDate) && formDate.before(afterDate)) {
                totalMoney = totalMoney + p.getPlatformFlowsBeautysalonMoney();
            }
        }
        DailyBudgetDto dailyBudgetDto = new DailyBudgetDto();
        dailyBudgetDto.setTotalMoney(totalMoney);
        return dailyBudgetDto;
    }

    /**
     * @description: 季度收入趋势数据
     * @param: quarter
     * @param: beautysalonId
     * @return: com.woniu.bh.beautysalon.model.dto.QuarterIncomeDto
     */
    @Override
    public QuarterIncomeDto quarterIncome(Integer quarter, Integer beautysalonId) {
        QueryWrapper<PlatformFlows> platformFlowsQueryWrapper = new QueryWrapper<>();
        platformFlowsQueryWrapper.eq("platform_flows_beautysalon_id", beautysalonId);
        List<PlatformFlows> platformFlows = platformFlowsMapper.selectList(platformFlowsQueryWrapper);
        QuarterIncomeDto quarterIncomeDto = new QuarterIncomeDto();
        List<Double> doubles = new ArrayList<>();
        for (PlatformFlows p : platformFlows) {
            Calendar instance = Calendar.getInstance();
            instance.setTime(new Date(p.getPlatformFlowsTime()));
            if ((instance.get(Calendar.MONTH) / 3 + 1) == quarter) {
                if (DateUtils.getMonth(new Date(p.getPlatformFlowsTime())) == (quarter * 3 - 2)) {
                    double totalMoney1 = 0;
                    totalMoney1 = totalMoney1 + p.getPlatformFlowsBeautysalonMoney();
                    doubles.add(totalMoney1);
                } else if (DateUtils.getMonth(new Date(p.getPlatformFlowsTime())) == (quarter * 3 - 1)) {
                    double totalMoney2 = 0;
                    totalMoney2 = totalMoney2 + p.getPlatformFlowsBeautysalonMoney();
                    doubles.add(totalMoney2);
                } else if (DateUtils.getMonth(new Date(p.getPlatformFlowsTime())) == (quarter * 3)) {
                    double totalMoney3 = 0;
                    totalMoney3 = totalMoney3 + p.getPlatformFlowsBeautysalonMoney();
                    doubles.add(totalMoney3);
                }
            }
        }
        quarterIncomeDto.setTotalMoney(doubles);
        return quarterIncomeDto;
    }

    /**
     * @description: 美容馆分成
     * @param: param
     * @param: beautysalonId
     * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.woniu.bh.beautysalon.model.dto.BeautysalonMoneyDto>
     */
    @Override
    public IPage<BeautysalonMoneyDto> getMyMoney(BeautysalonPlatformflowsParam param, Integer beautysalonId) {
        Page<BeautysalonMoneyDto> page = new Page<>(param.getPageIndex(), param.getPageSize());
        return platformFlowsMapper.getMyMoney(page,param.getAfterDate(), param.getBeforeDate(),param.getType(),beautysalonId);
    }
}
